var evaluation = {};
//快速估价
evaluation.evaluationBut=function(){
	$("#evaluationForm").find(".pop-mask").show();
	$("#evaluationForm").find("#evaluation").addClass("show");
}
//开始估价
evaluation.startEvaluation=function(){
	if(!$("#evaluationForm").valid()){
		return ;
	}
	var param = $("#evaluationForm").serialize();
	$.ajax({
	  type : "post",  
      dataType : "json",  
      data :param,
      async: false,
      url :baseUrl + "/evaluation/evaluationIndex",  
      success : function(data) {
      	$("#evaluationResult").find(".tit").html(data.estate);
      	$("#evaluationResult").find(".hall").html(data.houseType);
      	$("#evaluationResult").find("#evaluation_area").html(data.rea);
      	$("#evaluationResult").find("#evaluation_orientation").html(data.orientation);
      	$("#evaluationResult").find("#evaluation_floorTotal").html(data.floor+"层(共"+data.floorTotal+"层)");
      	if(!data.assessTotalPrice){
      		$("#nullPrice").show();
      	}else{
          	$("#evaluationResult").find("[lang='totalPrice']").html(data.assessTotalPrice);
          	$("#evaluationResult").find("[lang='totalWc']").html("误差在"+data.minTotalPrice+"万    - "+data.maxTotalPrice+"万之间");
          	$("#evaluationResult").find("[lang='price']").html(data.assessPrice);
          	$("#evaluationResult").find("[lang='priceWc']").html("误差在"+data.minPrice+"元/平    - "+data.maxPrice+"元/平之间");
          	$("#showPrice").show();
      	}
      }
	})
	$("#evaluation").removeClass("show");
	$("#evaluationResult").addClass("show");
}
evaluation.wantSale = function (){
	$("#evaluationForm").find(".pop-close").click();
	if(wantSale && wantSale.ensureSaleBut){
		wantSale.ensureSaleBut('')
	}
}
//快速估价表单校验
$(function (){
	$("#evaluationForm").validate({
		ignore: '',
		onkeyup: false,
     	rules: {
     		estate: {
     			required: true,
     			maxlength: 50
     		},
     		area: {
     			required: true,
     			evaluation_area_check:true
     		},room:{
     			required: true,
     		},orientation:{
     			required: true,
     		},floor:{
     			required: true,
     		},floorTotal:{
     			floor_check:true,
     			required: true,
     		}
      },
      errorPlacement: function(error, element) {
    	  var msgArea;
    	  msgArea = element.parent().parent().find("span[class*='li_pop_tishi']");
    	  $(msgArea).attr('data-original-title',error.html());
    	  $(msgArea).attr('title',error.html());
    	  msgArea.show();
    	  $(msgArea).tooltip();
      },
      success: function(label,element) {
    	  var msgArea;
    	  msgArea = $(element).parent().parent().find("span[class*='li_pop_tishi']");
    	  msgArea.hide();
    	  $(msgArea).tooltip('destroy');
	  }
	});
	
	$("#evaluationForm").find(".pop-close").click(function(){
		$("#evaluationForm").find(".pop-mask").hide();
		$(this).parent().removeClass("show");
		$("#evaluationForm")[0].reset();
		$("#evaluationForm").find(".li_pop_tishi").hide();
		//清空街道下拉
		$("#evaluationForm").find(".chosen-select").trigger("chosen:updated");
		$("#evaluationForm").find(".chosen-select-unit").trigger("chosen:updated");
		
		$("#showPrice").hide();$("#nullPrice").hide();
	})
	//面积
    $.validator.addMethod("evaluation_area_check", function(value, element, params) {
        if ((value != '') || (value != undefined)) {
            var regax = /^\d{1,11}(\.\d{1,2})?$/;
            return this.optional(element) || (regax.test(value));
        }
        return true;
    }, "请输入正确的面积,最多2位小数");
   //楼层校验 
    $.validator.addMethod("floor_check", function(value, element, params) {
        if ((value != '') || (value != undefined)) {
        	if($("#evaluationFloor").val()!=""){
        		if(parseInt(value)<parseInt($("#evaluationFloor").val())){
        			  return false;
        		}
        	}
        }
        return true;
    }, "所在楼层大于总楼层");
    //小区
  	$("#evaluationForm #evaluationEstate").autocomplete({
  		source : function(request, response) {
  			if(request.term==""){
  				return false;
  			}
  			$.ajax({
  				url:baseUrl + "/plot/getPlotList",
  				dataType : "json",
  				type : 'post',
  				data : {
  					'matchStr' : request.term,
  					cityCode:$("#nowCity").attr("citycode")
  				},
  				success : function(data) {
  					var arr = new Array(data.length);
  					for (var i = 0; i < data.length; i++) {
  						var s = {
  							value : data[i].plotName,
  							key: data[i].id,
  						};
  						arr[i] = s;
  					}
  					response(arr); 
  				}
  			});
  		},
  		select : function(event, ui) {
  			$("#evaluationForm #evaluationEstate").val(ui.item.value);
  			$("#evaluationForm #evaluationEstateId").val(ui.item.key);
  			event.preventDefault();
  		},
  		change: function( event, ui ) {
  	        if(ui.item == null){
  	        	$("#evaluationForm #evaluationEstate").val("");
  	        	$("#evaluationForm #evaluationEstateId").val("");
  	        }
  	    }
  	});
  	
  	$("#evaluationForm").find(".select").click(function(){
  		$("#evaluationForm").find(".conselect").removeClass("active");
		$(this).parent().addClass("active");
	})
	
	//select下拉框选择
	$("#evaluationForm").find(".select").next().find("li[class='com-li']").click(function(){
		$(this).parent().find("li").removeClass("selected");
		$(this).addClass("selected");
		$(this).parents("div[class='p-r']").prevAll().val($(this).text());
		$(this).parents("div[class='p-r']").parent().removeClass("active");
	})
	
	//select下拉框鼠标移除关闭
	$("#evaluationForm").find(".select").parent().mouseleave(function(){
		$(this).removeClass("active");
	});
})
	